import numpy as np
from scipy import *

import math
import random

import pdb

# --------------------   Generate Inhomogeneous Poisson Processtime events

def inhom_poisson(rate,t,rmax=1000.):
    # rate is the rate of the poisson process. If it's a number, the process is homogeneous, if it's a vector has to have the same size as t
    # t is a vector containing all the time bins
    # rmax is the maximu rate that the poisson process can have

    

    dt=t[2]-t[1];

    if size(rate)==1:
        rgen=rate;

    elif size(rate)==size(t):
        rgen=rmax;

    else:
        print 'Error, the size of the rate is neither 1 nor equal to the size of the time'

    # generate spiketimes accoring to the maximum rate
    tspike=np.array([0.]);        

    while tspike[-1]<t[-1]:
        
        interval=random.expovariate(rgen);
        #pdb.set_trace()
        tspike=concatenate((tspike,np.array([tspike[-1]+interval])),1);

    tspike=np.delete(tspike, [0])
    tspike=np.delete(tspike, [size(tspike)-1])
    
    
    # generate the inhomogeneous poisson spikes
    if size(rate)!=1:
        
        spikebin=np.rint(tspike/dt);
        spikebin=spikebin.astype(int);
        
        rnorm=rate[spikebin]/rmax;
        rtest=np.random.random_sample(size(rnorm));

        spikebin=spikebin[rtest<rnorm]
        tspike=spikebin*dt;


    return tspike
